#!/bin/sh

argv0=`basename $0`
if [ $# -ne 1 ]; then
  exec /sbin/umount $*
fi

# if target is the mount point
target=$1
if [ -d "$target" ]; then
  realpath=`realpath $target`
  md=`/sbin/mount | grep "/dev/md.* on $realpath (cd9660" | awk '{print $1}' | awk -F/ '{print $3}' | tail -1`
  if [ ! -z "$md" ]; then
    /sbin/umount $realpath
    if [ $? -ne 0 ]; then
      exit $?
    fi
    /sbin/mount | grep -q "^/dev/$md "
    if [ $? -ne 0 ]; then
      /sbin/mdconfig -d -u $md
      exit $?
    fi
  fi
fi

# if target is the mount point
for md in `/sbin/mdconfig -l`; do
  /sbin/mdconfig -l -u $md | grep -q $target
  if [ $? -eq 0 ]; then
    /sbin/umount `/sbin/mdconfig -l -u $md | grep $target | awk '{print $4}'` && /sbin/mdconfig -d -u $md
    exit $?
  fi
done

# call the real umount
/sbin/umount $*
